package net.liteheaven.xblib.common.io.reborn;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * Created by liangy on 2017/1/23.
 */
public class RebornSpHelper {

    private static int lookFor(Context context, String type){
        SharedPreferences sp = context.getSharedPreferences(type, Context.MODE_PRIVATE);
        for (int index = 1; index <= RebornConfig.MAX_ACTION; ++index){
            boolean contains = sp.contains("" + index);
            if (!contains){
                return index;
            }
        }
        return 0;
    }

    public static boolean push(Context context, String type, String str){
        int index = lookFor(context, type);
        if (index <= 0){
            return false;
        }

        String key = "" + index;
        SharedPreferences sp = context.getSharedPreferences(type, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, str);
        editor.apply();
        return true;
    }

    public static Map<String, ?> popAll(Context context, String type){
        SharedPreferences sp = context.getSharedPreferences(type, Context.MODE_PRIVATE);
        Map<String, ?> valueMap = sp.getAll();
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
        return valueMap;
    }
}
